from py3db.mysql import MySql
from app.get_app_info import AppData
from parse.parse import Parse
from config.db import DB_CONFIG
from config.app import APP_CODE
from fetch.async_fetch import AsyncRequest
from fetch.generate_urls import GenerateUrls
from framework.translate import Translate
import json
from os import system
from framework.db_frame import frame_run

class AppManager:

    def __init__(self, pub_code):
        self.app_data = AppData()
        self.app_msg, self.layout_result, self.field_list = self.app_data.get_one_info(pub_code)
        self.url_generate = GenerateUrls(pub_code)
        self.translate = Translate()
        self.db = MySql(DB_CONFIG['host'], DB_CONFIG['user'], DB_CONFIG['password'], pub_code)

    def get_app_data_by_table(self, table_name, page: int = 0, limit: int = 10):
        sql = f"select id,next_layout from {table_name} where next_layout is not NULL limit {page * limit + 1},{limit}";
        result = self.db.query(sql)
        return result

    def get_field_list(self, layout):
        field_dict = dict()
        for field in self.field_list:
            layout_id = int(field[-3])
            if layout_id == layout[0]:
                english_name = self.translate.get_english_word(field[1]).result
                temp_field = field[5] if field[5] else None
                temp_field_dict = {
                    english_name: {
                        "value": field[2],
                        "filter": temp_field,
                        "type": field[3]

                    }
                }
                if not field_dict.get(field[6]):
                    field_dict[field[6]] = temp_field_dict
                else:
                    field_dict[field[6]].update(temp_field_dict)
        return field_dict

    def __repr__(self):
        return f'''
            {self.app_msg},
            {self.layout_result},
            {self.field_list}
        '''

    def run(self):
        for layout in self.layout_result:
            field_dict = self.get_field_list(layout)
            print(field_dict)
            next_layout_info = self.app_data.get_layout_info_by_id(layout[7])
            parser_function = "parse"
            parser = Parse(
                item=field_dict,
                next_url_rule=layout[6],
                next_layout_info=next_layout_info,
                app_info=self.app_msg
                )
            url_list = self.url_generate.make_url(layout_info=layout, filed_dict=field_dict)
            # with open("url.json", "a") as fp:
            #     fp.write(str(url_list)+"\n")
            send_index, async_number = 1, 10
            urls = url_list[0: async_number]
            while len(urls):
                print("urls:",urls)
                req = AsyncRequest(
                    url_list=urls,
                    parse_func=getattr(parser, parser_function),
                    method=layout[3],
                    encoding=self.app_msg[6],
                    headers=json.loads(self.app_msg[5]),
                    parse_method=layout[5]
                )
                send_index += 1
                req.run()
                start = send_index * async_number

                try:
                    print("urls=", urls)
                    urls = url_list[start:start + async_number]
                except Exception as e:
                    print("urls=", urls)
                    break


if __name__ == '__main__':
    #frame_run()
    app = AppManager(APP_CODE)
    app.run()
